﻿using PMS.XCMG.Entity;
using PMS.XCMG.IBusiness;
using PMS.XCMG.Models;
using PMS.XCMG.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace PMS.XCMG.Api.Controllers
{
    /// <summary>
    /// Sys_User
    /// </summary>
    public class Sys_UserController : BaseController
    {
        public ISys_UserBusiness Bus { get; set; }
        public IServiceProvider SvcProvider { get; set; }
        public Sys_UserController(ISys_UserBusiness bus, IServiceProvider svcProvider)
        {
            this.Bus = bus;
            this.SvcProvider = svcProvider;
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public Task<Sys_User> GetAsync(string id)
        {
            return this.Bus.GetAsync(id, true);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public Task<PageResult<Sys_User>> GetPageAsync(PageInput<Sys_UserQM> query)
        {
            return this.Bus.GetPageResultAsync(query);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="data">对应数据</param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public Task<int> SaveAsync(Sys_User data)
        {
            this.InitBusEntity(data);
            return this.Bus.AddOrUpdateAsync(data);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">数据主键</param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public Task<int> DeleteAsync(List<string> ids)
        {
            return this.Bus.DeleteAsync(ids);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        [HttpPatch]
        public Task<int> UpdateStatusAsync(string id, string status)
        {
            var operatorSvc = this.SvcProvider.GetRequiredService<IOperator>();
            return this.Bus.UpdateAsync(id, new { Status = status, ModifyTime = DateTime.Now, ModifyUserId = operatorSvc.UserId });
        }

        /// <summary>
        /// 更新当前用户默认仓库
        /// </summary>
        /// <param name="whseId">仓库</param>
        /// <returns></returns>
        [HttpPatch]
        public Task<int> UpdateCurWhseAsync(string whseId)
        {
            var operatorSvc = this.SvcProvider.GetRequiredService<IOperator>();
            return this.Bus.UpdateAsync(operatorSvc.UserId, new { WhseId = whseId, ModifyTime = DateTime.Now, ModifyUserId = operatorSvc.UserId });
        }

        /// <summary>
        /// 更新当前用户默认货主
        /// </summary>
        /// <param name="storerId">货主</param>
        /// <returns></returns>
        [HttpPatch]
        public Task<int> UpdateCurStorerAsync(string storerId)
        {
            var operatorSvc = this.SvcProvider.GetRequiredService<IOperator>();
            return this.Bus.UpdateAsync(operatorSvc.UserId, new { StorerId = storerId, ModifyTime = DateTime.Now, ModifyUserId = operatorSvc.UserId });
        }
    }
}
